<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15 0015
 * Time: 上午 10:46
 */

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;

class Setting extends BasicAdmin
{

    public $table = 'Setting';

    /**友情链接设置
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function link(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $post['value'] = array_values($post['value']);
            if (!empty($post['value'])){
                foreach ($post['value'] as $k=>$v){
                    if (isset($v['status']) && $v['status']=='on'){
                        $post['value'][$k]['status'] = 1;
                    }else{
                        $post['value'][$k]['status'] = 2;
                    }
                }
                $post['value'] = json_encode($post['value'],320);
            }
            $post['name'] = 'link';
            DataService::save($this->table,$post,'name');
            $this->success('保存成功','');
        }
        $db = Db::name($this->table)->where(['name'=>'link'])->find();
        $db = $this->setting_data_filter($db);
        $this->assign([
            'title'=>'友情链接',
            'db'=>$db
        ]);
        return view();
    }

    protected function setting_data_filter($db){
        $value = json_decode($db['value'],true);
        $db['value'] = $value;
        return $db;
    }

    /**联系我们
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function contactus(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $post['value'] = array_values($post['value']);
            if (!empty($post['value'])){
                foreach ($post['value'] as $k=>$v){
                    if (isset($v['status']) && $v['status']=='on'){
                        $post['value'][$k]['status'] = 1;
                    }else{
                        $post['value'][$k]['status'] = 2;
                    }
                }
                $post['value'] = json_encode($post['value'],320);
            }
            $post['name'] = 'contactus';
            DataService::save($this->table,$post,'name');
            $this->success('保存成功','');
        }
        $db = Db::name($this->table)->where(['name'=>'contactus'])->find();
        $db = $this->setting_data_filter($db);
        $this->assign([
            'title'=>'联系我们',
            'db'=>$db
        ]);
        return view();
    }

    /**平台客服&二维码
     * @param Request $request
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function kefu(Request $request)
    {
        if ($request->isPost()) {
            $post = $request->post();
            foreach ($post as $k => $v) {
                (isset($v['status'])&&$v['status']=='on') ? $v['status']=1 : $v['status']=2;
                DataService::save($this->table,$v,'name');
            }
            $this->success('保存成功！', '');
        }
        $this->assign([
            'title' => '热线电话&二维码'
        ]);
        return view();
    }

    public function web_msg(Request $request){
        if ($request->isPost()) {
            $post = $request->post();
            foreach ($post as $k => $v) {
                (isset($v['status'])&&$v['status']=='on') ? $v['status']=1 : $v['status']=2;
                DataService::save($this->table,$v,'name');
            }
            $this->success('保存成功！', '');
        }
        $this->assign([
            'title' => '网站信息'
        ]);
        return view();
    }


    /**顶部logo
     * @var string
     */
    public $topimg = 'TopImg';
    public function top_img(){
        $this->title = '底部logo图设置';
        $db = Db::name($this->topimg)->where('is_deleted',1)->order('sort asc,id asc');
        return parent::_list($db);
    }

    public function add(){
        return $this->_form($this->topimg);
    }

    public function del(){
        if (DataService::update($this->topimg)){
            $this->success('删除成功！','');
        }else{
            $this->error('操作失败，请稍候重试！','');
        }
    }

    public function forbid(){
        $post = $this->request->post();
        Db::name($this->topimg)->where('id',$post['id'])->update(['status'=>2]);
        $this->success('禁用成功！','');
    }

    public function resume(){
        $post = $this->request->post();
        Db::name($this->topimg)->where('id',$post['id'])->update(['status'=>1]);
        $this->success('启用成功！','');
    }

    public function map(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $post['list'] = json_encode($post['list'],320);

            foreach ($post as $k=>$v){
                DataService::save($this->table,['name'=>$k,'value'=>$v],'name');
            }
            $this->success('保存成功！','');
        }
        $db = setting('list');
        $this->assign([
            'title'=>'地图&咨询信息',
            'db'=>json_decode($db,true)
        ]);
        return view();
    }
}